

//obj--shoppingStyle index
//date:2017-9-10
//author:Elementye
//index.js

$(function(){
   // $.cookie("spInfor",JSON.stringify({"system":"spStyle"}));
    //判断当前是否有用户登录

    if($.cookie("spInfor")){
        var currentUser = JSON.parse($.cookie("spInfor")).username;
        console.log(currentUser);
        //获取到当前用户的信息
        $(".overTop_left>#login").remove();
        $(".overTop_left>#regist").remove();
        var userSpan = document.createElement("span");
        var exitA = document.createElement("a");
        userSpan.innerHTML = currentUser;
        exitA.innerHTML = "退出";
        $(".overTop_left").append($(userSpan)).append($(exitA));

        $(exitA).on("click",function(){
            $.removeCookie("spInfor");
            window.location.reload();
        })
        var jug = {
            url:"../hz_1706/spUser/version1/getcar.php",
            data:{"phonenumber":currentUser},
            type:"post",
            dataType:"json",
            success:function(res){
                var spss = 0;
                if(!res.status){
                    //购物车中有数据
                    spss = res.length;
                }
                $("#spcar>span").html(spss);


            }
        }
        $.ajax(jug)


    }else{
        //未获取到用户信息
        console.log("未获取到用户信息");
        var spss = 0;
        if($.cookie("goodsses")){
            console.log($.cookie("goodsses"));
            spss=JSON.parse($.cookie("goodsses")).length;
        }
        $("#spcar>span").html(spss);

    }



    var oCir = document.getElementsByTagName("ol");






    //为banner动态添加小圈圈到ol中
    for(var i=0; i< $("#banner>ul>li").length; i++){
        var tempLi = document.createElement("li");
        oCir[0].append(tempLi);
    }
    $("#banner>ol>li")[0].className = "active";
    //克隆banner中的第一页追加到末尾以方便定时器实现轮播效果
    $("#banner>ul>li").eq(0).clone(true).appendTo($("#banner>ul"));
    var flag = 1;
    function autoPlay(){
        if(flag==10){
            flag = 1;
            $("#banner>ul")[0].style.left = 0;
        }
        $("#banner>ol>li").each(function(index, ele){
            ele.className="";
            if(index==flag%9){
                ele.className = "active";
            }
        })
        $("#banner>ul").animate({"left":-flag*$("#banner>ul>li")[0].offsetWidth},500);
        flag++;
    }
    var autoClock = setInterval(autoPlay,2000); //开启banner 的autoplay定时器
    $("#banner").on("mouseenter",function(){

        clearInterval(autoClock);
    });
    $("#banner").on("mouseleave",function(){
        autoClock = setInterval(autoPlay,2000);
    })
    //委托ol绑定小圈圈的触发轮播事件
    $("#banner>ol").on("click","li",function(){
        $(this).attr("class","active").siblings("li").attr("class","");
        flag = $(this).index()+1;
        $("#banner>ul").animate({"left":-$(this).index()*$("#banner>ul>li")[0].offsetWidth},500,function(){
            console.log(flag);
        })

    })




    //和banner类似，在recom的目录再开启一个轮播

    var vop = 1;
    function autoRec(){
        if(vop==4){
            vop = 1;
            $("#recBanner>ul")[0].style.left = 0;
        }
        $("#recBanner>ol>li").each(function(index, ele){
            ele.className="";
            if(index==vop%3){
                ele.className = "sel";
            }
        })
        $("#recBanner>ul").animate({"left":-vop*$("#recBanner>ul>li")[0].offsetWidth},500);
        vop++;
    }
   var recClock = setInterval(autoRec,2000); //开启banner 的autoplay定时器
    $("#recBanner").on("mouseenter",function(){

        clearInterval(recClock);
    });
    $("#recBanner").on("mouseleave",function(){
        recClock = setInterval(autoRec,2000);
    })
    $("#recBanner>ol").on("click","li",function(){
        $(this).attr("class","sel").siblings("li").attr("class","");
        vop = $(this).index()+1;
        $("#recBanner>ul").animate({"left":-$(this).index()*$("#recBanner>ul>li")[0].offsetWidth},500)

    })




})
